package com.ruoyi.video.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.video.domain.VideoServer;
import com.ruoyi.video.service.IVideoServerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务Controller
 * 
 * @author ruoyi
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/video/server")
public class VideoServerController extends BaseController
{
    @Autowired
    private IVideoServerService videoServerService;

    /**
     * 查询服务列表
     */
    @PreAuthorize("@ss.hasPermi('video:server:list')")
    @GetMapping("/list")
    public TableDataInfo list(VideoServer videoServer)
    {
        startPage();
        List<VideoServer> list = videoServerService.selectVideoServerList(videoServer);
        return getDataTable(list);
    }

    /**
     * 导出服务列表
     */
    @PreAuthorize("@ss.hasPermi('video:server:export')")
    @Log(title = "服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VideoServer videoServer)
    {
        List<VideoServer> list = videoServerService.selectVideoServerList(videoServer);
        ExcelUtil<VideoServer> util = new ExcelUtil<VideoServer>(VideoServer.class);
        util.exportExcel(response, list, "服务数据");
    }

    /**
     * 获取服务详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:server:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoServerService.selectVideoServerById(id));
    }

    /**
     * 新增服务
     */
    @PreAuthorize("@ss.hasPermi('video:server:add')")
    @Log(title = "服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VideoServer videoServer)
    {
        videoServer.setCreateBy(getUsername());
        return toAjax(videoServerService.insertVideoServer(videoServer));
    }

    /**
     * 修改服务
     */
    @PreAuthorize("@ss.hasPermi('video:server:edit')")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VideoServer videoServer)
    {
        videoServer.setUpdateBy(getUsername());
        return toAjax(videoServerService.updateVideoServer(videoServer));
    }

    /**
     * 删除服务
     */
    @PreAuthorize("@ss.hasPermi('video:server:remove')")
    @Log(title = "服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videoServerService.deleteVideoServerByIds(ids));
    }


    /**
     * 启动服务
     */
    @PreAuthorize("@ss.hasPermi('video:server:list')")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @GetMapping("/start/{code}")
    public AjaxResult start(@PathVariable String code)
    {
        return toAjax(videoServerService.triggerVideoServer(code, "start"));
    }



    /**
     * 停止服务
     */
    @PreAuthorize("@ss.hasPermi('video:server:list')")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @GetMapping("/stop/{code}")
    public AjaxResult stop(@PathVariable String code)
    {
        return toAjax(videoServerService.triggerVideoServer(code, "start"));
    }
}
